""" 1_2 联网功能 """
# https://learn.adafruit.com/adafruit-esp32-s3-tft-feather/circuitpython-internet-test

import ssl
import wifi
import socketpool
import ipaddress
import adafruit_requests	# 需要安装扩展
import sys

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"

secrets = {
    "ssid" : "OpenBSD",
    "password" : "13581882013"
}

print("ESP32-S3 网络功能测试")

print("开发板MAC地址:", [hex(i) for i in wifi.radio.mac_address])

print("扫描WiFiw网络:")
for network in wifi.radio.start_scanning_networks():
    print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
            network.rssi, network.channel))
wifi.radio.stop_scanning_networks()

print("连接 %s"%secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("连接到 %s!"%secrets["ssid"])
print("开发板IP地址", wifi.radio.ipv4_address)

ipv4 = ipaddress.ip_address("114.114.114.114")
print("Ping 114.114.114.114: %f ms" % (wifi.radio.ping(ipv4)*1000))

# sys.exit()

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

print("网络请求网址", TEXT_URL)
response = requests.get(TEXT_URL)
print("请求返回内容")
print("-" * 40)
print(response.text)
print("-" * 40)

print("测试完成")
